<template>
	<view class="content">
		<u-navbar bgColor="#fff" :placeholder="true" :fixed="true" @leftClick="$back" title="dream约文" titleStyle="{'color': '#000'}">
			
		</u-navbar>
		<view class="baibox">
			<view class="yintxt">
				举报原因
			</view>
			<input type="text" class="put" placeholder="请输入" v-model="form.reason">
			<view class="yintxt">
				描述详情
			</view>
			<input type="text" class="put" placeholder="填写相关证据，例如: 具体违规内容、使用模板信息等" v-model="form.row">
			
			<view class="tutxt">
				凭证图片<text>0/8</text>
			</view>
			<view class="imgView">
				<!-- <view v-for="(img,index) in form.img" class="imgList">
					<image :src="img | formatImgUrl" mode="aspectFill" style="width: 190rpx;height: 190rpx;"></image>
					<view class="imgClose" @click="deletePic(index)">×</view>
				</view>
				<image :src="'/images/up.png' | formatImgUrl" mode="widthFix" style="width: 190rpx;height: 190rpx;"
					@click="onChooseImg" v-if="form.img.length < 8"></image> -->
					
				<u-upload ref="uUpload" :uploadIcon="'/images/up.png' | formatImgUrl" :multiple="true" maxCount="8" width="78" height="78" :action="uploadImgUrl" :fileList="form.img" @afterRead='afterRead' @delete="deletePic">
				</u-upload>
			</view>
		</view>
		<view class="subbox" @click="submit">
			提交
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				form:{
					aid:0,
					type:1,
					img:[],
					flag:"",
				},
				fileList: [],
				fileList2: []
			}
		},
		onLoad(e) {
			console.log(e,7777777777)
			this.form.aid = e.id;
			this.form.type = e.type;
			this.form.flag = e.sid;
			
			
			// if(e.id){
			// 	this.form.aid = e.id
			// 	if(e.type){
			// 		this.form.type = e.type
			// 	}else{
			// 		this.form.type = 1
			// 	}
			// }else{
			// 	this.$common.errorToShow('参数异常')
			// }
			// console.log(e)
		},
		methods: {
			submit(){
				this.form.img = this.fileList2
				
				this.$api.default.request('user/addReport',this.form).then((res) => {
					if (res.code) {
						this.$common.successToShow(res.msg,()=>{
							uni.navigateBack()
						})
					}
				})
			},
			// 删除图片
			deletePic(index) {
				this.form.img.splice(index, 1)
				this.fileList2.splice(index, 1)
			},
			onChooseImg() {
				this.$common.chooseImage({}, (data) => {
					if (data) {
						this.form.img.push(data)
					}
				}, 8)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.form.img.length
				lists.map((item) => {
					this.form.img.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.form.img[fileListLen]
					this.form.img.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.uploadImgUrl, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							let datas = JSON.parse(res.data)
							this.fileList2.push(datas.url)
							setTimeout(() => {
								resolve(datas.url)
							}, 300)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: rgba(242, 245, 242, 1);
	}

	.content {
		padding-top: 20rpx;

		.baibox {
			background-color: #ffffff;
			padding: 40rpx 30rpx 20rpx;
			margin-bottom: 40rpx;
			.yintxt{
				font-size: 28rpx;
				font-weight: bold;
				letter-spacing: 0em;
				color: #3D3D3D;
				padding-bottom: 8rpx;
			}
			.put {
				width: 690rpx;
				font-size: 24rpx;
				color: #3d3d3d;
				padding: 0 0 20rpx;
				border-bottom: 1px solid rgba(153, 153, 153, 0.1);
				margin-bottom: 20rpx;
			}

			.tutxt{
				font-size: 28rpx;
				font-weight: bold;
				letter-spacing: 0em;
				color: #3D3D3D;
				padding-bottom: 20rpx;
				text{
					font-size: 24rpx;
					font-weight: normal;
					letter-spacing: 0em;
					color: #999999;
					padding-left: 12rpx;
				}
			}
			
			.imgView {
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				.imgList:nth-child(3n){
					margin-right: 0;
				}
				.imgList {
					position: relative;
					padding-bottom: 10rpx;
					margin-right: 31rpx;
					.imgClose {
						position: absolute;
						top: -8px;
						right: -8px;
						z-index: 1;
						width: 36rpx;
						height: 36rpx;
						background-color: #45C4B0;
						color: #fff;
						line-height: 36rpx;
						text-align: center;
						border-radius: 100px;
					}
				}
			
				.imgList:not(:first-child) {
					// margin-left: 15rpx;
				}
			}
			
			.zebox{
				margin-top: 20rpx;
				height: 38rpx;
				border-radius: 18px;
				opacity: 1;
				background: rgba(153, 153, 153, 0.1);
				display: flex;
				align-items: center;
				justify-content: center;
				width: 170rpx;
				.zepic{
					width: 32rpx;
					height: 32rpx;
				}
				.zetxt{
					font-size: 24rpx;
					font-weight: normal;
					letter-spacing: 0.08em;
					color: #3D3D3D;
					padding-left: 8rpx;
				}
			}
		}
		.subbox{
			width: 630rpx;
			height: 86rpx;
			border-radius: 8px;
			opacity: 1;
			background: #45C4B0;
			text-align: center;
			margin: 0 auto;
			line-height: 86rpx;
			font-size: 32rpx;
			font-weight: bold;
			color: #FFFFFF;
		}
	}
</style>